package com.springboot.curator.demo;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessLock;
import org.apache.curator.framework.recipes.locks.InterProcessMultiLock;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;

import java.util.ArrayList;
import java.util.List;

/**
 * Curator 框架测试类
 *
 * @author: zr
 * @date: 2021-09-12
 */
public class CuratorDemoApplication {

    public static void main(String[] args) throws Exception {
        String zookeeperConnectionString = "192.168.0.201:2181,192.168.0.202:2181,192.168.0.203:2181";
        // 设置重试次数
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);
        client.start();

        // zookeeper 创建一个节点
        // client.create()
        //         .creatingParentsIfNeeded()
        //         .forPath("/my/path", "hello world".getBytes());
        //
        // byte[] bytes = client.getData().forPath("/my/path");
        //
        // System.out.println("/my/path 节点下存储的数据是：" + new String(bytes));

        // InterProcessMutex lock = new InterProcessMutex(client, "/locks/lock-01");
        // lock.acquire();
        // Thread.sleep(30000);
        // lock.release();

        /** Semaphore 的 用法*/
        // InterProcessSemaphoreV2 semaphoreV2 = new InterProcessSemaphoreV2(client, "/semaphores/semaphore_01", 3);
        // Lease lease = semaphoreV2.acquire();
        // Thread.sleep(30000);
        // semaphoreV2.returnLease(lease);

        /** 非可重入锁的用法*/
        // InterProcessSemaphoreMutex semaphoreV2 = new InterProcessSemaphoreMutex(client, "/semaphores/semaphore_01");
        // semaphoreV2.acquire();
        // Thread.sleep(30000);
        // semaphoreV2.release();

        /** 读写锁的用法*/
        // InterProcessReadWriteLock semaphoreV2 = new InterProcessReadWriteLock(client, "/locks/lock_01");
        // /** 加写锁*/
        // semaphoreV2.writeLock().acquire();
        // /** 加读锁*/
        // semaphoreV2.readLock().acquire();
        // Thread.sleep(30000);
        /** 释放读锁*/
        // semaphoreV2.readLock().release();

        // Thread.sleep(30000);
        /** 释放写锁*/
        // semaphoreV2.writeLock().release();

        InterProcessMutex lock1 = new InterProcessMutex(client, "/locks/lock-01");
        InterProcessMutex lock2 = new InterProcessMutex(client, "/locks/lock-02");
        InterProcessMutex lock3 = new InterProcessMutex(client, "/locks/lock-03");
        List<InterProcessLock> locks = new ArrayList();
        locks.add(lock1);
        locks.add(lock2);
        locks.add(lock3);

        InterProcessMultiLock multiLock = new InterProcessMultiLock(locks);
        multiLock.acquire();
        multiLock.release();
    }
}
